package com.campus.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @desc: 全局异常处理类
 * @author: hjm
 * @date: 2024/11/22
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理业务异常
     * @param ex 业务异常
     * @return 响应实体，包含异常信息
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<String> handleUserNotFoundException(BusinessException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
    }

    /**
     * 处理未找到处理器异常
     * @param ex 未找到处理器异常
     * @return 响应实体，包含异常信息
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseEntity<String> handleNoHandlerFoundException(NoHandlerFoundException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到请求的处理器");
    }

    /**
     * 处理通用异常
     * @param ex 通用异常
     * @return 响应实体，包含异常信息
     */
//    @ExceptionHandler(Exception.class)
//    public ResponseEntity<String> handleGenericException(Exception ex) {
//        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("发生意外错误");
//    }
}